<?php
/**
 *
 */
namespace app\home\controller\cart;

use app\home\controller\Base;

class Api extends Base
{
	/**
	 * 购物车列表
	 * @return [type] [description]
	 */
	public function cart(){
		$cookie = cookie('cartlist');
        $total_price = 0;
        if (!$cookie)return json(['code'=>0,'msg'=>'购物车暂无商品']);
        foreach ($cookie as $k => $v) {
            $cookie[$k]['goods'] = self::setRemark($k);
            $cookie[$k]['total'] = intval($v['number'])*intval($v['price']);
            $total_price += intval($cookie[$k]['total']);
        }
        return json(['code'=>1,'msg'=>'购物车列表查询成功','data'=>$cookie,'total_price'=>number_format($total_price,2)]);
	}

    /**
     * 清空购物车
     * @return [type] [description]
     */
    public function clearCart(){
        cookie('cartlist',null);
        return json(['code'=>1,'msg'=>'购物车清除成功']);
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public static function setRemark($gid){
        return model('MallGoods')->getOneDataById($gid,'title,content,unit,weight,photo,city,coin_id,credit');
    }
}